using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Objects;
using System.Data.Metadata.Edm;
using System.Data.Objects.DataClasses;
using TrucksDir.Data;

namespace TrucksDir.Data.Services
{
 public static class EntityObjectExtensions
 {
  public static string GetEntitySetName(this EntityObject entity)
  {
      using (var context = new trucksmodel())
   {
    var container = context.MetadataWorkspace.GetEntityContainer(context.DefaultContainerName, DataSpace.CSpace);
    string entitySetName = (from meta in container.BaseEntitySets
                            where meta.ElementType.Name == entity.GetType().Name
                            select meta.Name).First();
    return entitySetName;
   }
  }  

  public static void SubmitChanges(this EntityObject entity)
  {
   Context.Instance.DetectChanges();
   Context.Instance.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
  }
  public static void RejectChanges(this EntityObject entity)
  {
   Context.Instance.Refresh(RefreshMode.StoreWins, entity);
  }
  public static void Detach(this EntityObject entity)
  {
   Context.Instance.Detach(entity); 
  }
 }
}